package com.zut.dao;

import com.zut.domain.Exam;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ExamDao {

    @Results(id = "groupWithExam",
            value = {
                    @Result(property = "id", column = "exam_id", id = true),
                    @Result(property = "place", column = "exam_place"),
                    @Result(property = "subject", column = "exam_subject"),
                    @Result(property = "peoplelimit", column = "exam_peoplelimit"),
                    @Result(property = "peoplecurrent", column = "exam_peoplecurrent"),
                    @Result(property = "date", column = "exam_date")
            })
    @Select("select * from exam order by exam_date desc")
    List<Exam> getAll();

    @Insert("insert into exam (exam_id,exam_place,exam_subject,exam_peoplelimit,exam_peoplecurrent,exam_date) " +
            "values(#{id},#{place},#{subject},#{peoplelimit},#{peoplecurrent},#{date})")
    int insert(Exam exam);

    @Delete("delete from exam where exam_id = #{id}")
    int deleteById(String id);

    @Update("<script>                                                                        "
            + "update exam                                                                   "
            + "<set>                                                                         "
            + "  <if test='subject != null'> exam_subject=#{subject},                   </if>"
            + "  <if test='place != null'> exam_place=#{place},                         </if>"
            + "  <if test='peoplelimit != null'> exam_peoplelimit=#{peoplelimit},       </if>"
            + "  <if test='peoplecurrent != null'> exam_peoplecurrent=#{peoplecurrent}, </if>"
            + "  <if test='date != null'> exam_date=#{date},                            </if>"
            + "</set>                                                                        "
            + "where exam_id = #{id}                                                         "
            + "</script>                                                                     ")
    int update(Exam exam);

    @ResultMap("groupWithExam")
    @Select("select * from exam where exam_place = #{place}")
    List<Exam> getByPlace(String place);

    @ResultMap("groupWithExam")
    @Select("select * from exam where exam_subject = #{subject}")
    List<Exam> getBySubject(String subject);

    @ResultMap("groupWithExam")
    @Select("select * from exam where exam_subject = #{subject} and exam_date like #{date}")
    List<Exam> getExamMsg(String subject,String date);

    @ResultMap("groupWithExam")
    @Select("select * from exam where exam_id = #{id}")
    Exam getByExamId(String id);

    @ResultMap("groupWithExam")
    @Select("select * from exam where exam_id = #{id}")
    List<Exam> getByExamIdPage(String id);
}
